﻿ /*------------------------------------------------------------------
-- COPYRIGHT (C) 2010-2012  Atom
-- ALL RIGHTS RESERVED.
-- 兆尹科技
-- CREATE DATE: 2010/07/12
-- CREATE MAN:	姜新军
-- 验证实体类
-- MODIFY HISTORY:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
---------------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Text;

namespace Atom.Entity
{
    /// <summary>
    /// 验证实体类
    /// </summary>
    /// <typeparam name="TEntity">需要验证的实体类类型</typeparam>
    public class ValidatorEntity<TEntity>
    {
        private TEntity entity;

        private List<string> errorMsgs;

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> ErrorMessages
        {
            get
            {
                if (errorMsgs == null)
                {
                    errorMsgs = new List<string>();
                }
                return errorMsgs;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="entity"></param>
        public ValidatorEntity(TEntity entity)
        {
            this.entity = entity;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="predicate">错误成立条件</param>
        /// <param name="errorMsg">如果错误成立，显示的错误信息</param>
        /// <returns></returns>
        public ValidatorEntity<TEntity> Validate(Predicate<TEntity> predicate, string errorMsg)
        {
            if (predicate != null && predicate(entity))
            {
                this.ErrorMessages.Add(errorMsg);
            }
            return this;
        }
    }
}
